package com.example.quanly;

import java.security.PublicKey;
import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

public class MainActivity extends Activity {
	ArrayList<Employee>arrEmployee = new ArrayList<Employee>();
	// su dung MyArrayAapter
	MyArrayAdapter adapter = null;
	ListView lvNhanvien = null;
	
	Button btnNhap;
	ImageButton btnRemoveAll;
	EditText editMa, editTen;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnNhap=(Button) findViewById(R.id.btnNhap);
		btnRemoveAll=(ImageButton) findViewById(R.id.btndelete);
		editMa=(EditText) findViewById(R.id.editMa);
		editTen=(EditText) findViewById(R.id.editTen);

		lvNhanvien=(ListView) findViewById(R.id.lvnhanvien);
		arrEmployee=new ArrayList<Employee>();
		
		//Khởi tạo đối tượng adapter và gán Data source
		adapter = new MyArrayAdapter(this, R.layout.my_item_layout, arrEmployee);
		lvNhanvien.setAdapter(adapter);
		
		btnNhap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				xulyNhap();
			}
		});
		
		btnRemoveAll.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				xulyXoa();
			}
		});
		
	}

	public void xulyNhap(){
		String ma= editMa.getText() + " ";
		String ten = editTen.getText() + " ";
		Employee emp = new Employee();
		emp.setID(ma);
		emp.SetName(ten);
		
		arrEmployee.add(emp);
		adapter.notifyDataSetChanged();
		
		editMa.setText("");
		editTen.setText("");
		editMa.requestFocus();
	}
	
	public void xulyXoa(){
		for(int i =lvNhanvien.getChildCount()-1; i>=0;i--){
			View v= lvNhanvien.getChildAt(i);
			CheckBox chk= (CheckBox) v.findViewById(R.id.chkitem);
			if(chk.isChecked())
			{
				arrEmployee.remove(i);
			}
			adapter.notifyDataSetChanged();
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
